package com.monkey.server;

import java.util.function.Function;

import org.monkey.metrics.MetricsExecutor;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.monkey.model.Address;
import com.monkey.netty.server.BusinessExecutor;
import com.monkey.netty.server.NettyServer;
import com.monkey.server.config.ClusterProperties;
import com.monkey.server.sync.ClusterContext;
import com.monkey.server.sync.NodeSynchronizer;

/**
 * 启动时初始化 NettyServer，
 * 
 * @author zhouman
 *
 */
@SpringBootApplication(scanBasePackages = { "com.monkey.server" })
public class MonkeyServerApplication implements CommandLineRunner {
    @Autowired
    private ClusterProperties clusterProperties;

    @Autowired
    private BeanFactory beanFactory;
    
    public static void main(String[] args) {
        SpringApplication.run(MonkeyServerApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        MetricsExecutor metricsExecutor = new MetricsExecutor();
        metricsExecutor.printMemory();
//
//        ClusterContext.init(clusterProperties);
//
//        NettyServer nettyServer = new NettyServer();
//        nettyServer.setPort(clusterProperties.getNettyport());
//
//        BusinessExecutor businessExecutor = new BusinessExecutor();
//        businessExecutor.setBeanFactory(beanFactory);
//        nettyServer.setBusinessExecutor(businessExecutor);
//
//        if(args != null && args.length > 0) {
//            Function<Address, String> callback = new Function<Address, String>() {
//                @Override
//                public String apply(Address address) {
//                    nodeSynchronizer.join(address);
//                    return "";
//                }
//            };
//            Address anyNodeOfClusterAddress = new Address("10.3.0.110", 7000);
//            nettyServer.bind(callback, anyNodeOfClusterAddress);
//        } else {
//            nettyServer.bind();
//        }


    }

}
